<? 
class MY_Model extends CI_Model
{
	public $essense;

	function __construct()
	{
		parent::__construct();
	}
	
	function getById($id, $table = false)
	{
		if ($table === false)
			$table = $this->essense;

		$id = abs(intval($id));
		$this->db->where('id', $id);
		return $this->db->get($table)->result();
	}

	function delById($id, $table = false)
	{
		if ($table === false)
			$table = $this->essense;
		
		$this->db->where('id', $id);
		$this->db->delete($table);
	}

	function updaterDB($table = '', $data, $update = false)
	{
		$newData = new stdClass();

		if ($table == '')
			$table = $this->essense;
	

		$query="SELECT * FROM ".$table." WHERE 1 LIMIT 1";  
		$fieldsSQL =mysql_fetch_array(mysql_query($query), MYSQL_ASSOC);  
		foreach($fieldsSQL as $k=>$v)	$fieldsDB[]=$k; 

		$data = (array) $data;
		$keysData = array_keys($data);

		foreach ($keysData as $key => $val) 	
			if (!in_array($val, $fieldsDB))	unset($keysData[$key]);

		foreach ($keysData as $key => $val) 
			$newData->$val= $data[$val];


		if ($update) $id = $newData->id;
		unset($newData->id);
		

		if ($update)
		{
			$this->db->where('id', $id);
			return $this->db->update($table, $newData);
		}
		else
		{	
			return $this->db->insert($table, $newData);
		}
	}
}
?>